package web.commonApp.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SecurityInterceptor extends HandlerInterceptorAdapter{
	
	private static final Logger logger = Logger.getLogger(SecurityInterceptor.class);

		//before the actual handler will be executed
		public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler)
		    throws Exception {
	 
			long startTime = System.currentTimeMillis();
			
			logger.info("[" + handler + "] Start-time : " + startTime + "ms");
			
			request.setAttribute("startTime", startTime);
	 
			return true;
		}
	 
		//after the handler is executed
		public void postHandle(HttpServletRequest request, HttpServletResponse response,Object handler, ModelAndView modelAndView)
			throws Exception {
			
	 
			long startTime = (Long)request.getAttribute("startTime");
	 
			long endTime = System.currentTimeMillis();
	 
			logger.info("[" + handler + "] End-time : " + endTime + "ms");
			long executeTime = endTime - startTime;
	 
			//log it
			logger.info("[" + handler + "] executeTime : " + executeTime + "ms");
			
		}
}
